
/*!
 * \file      coloredsquare.cpp
 * \brief     ColoredSquare implementation.
 * \author    Duriez Franck
 * \author    Carton Paul-Henri
 * \version   0.1
 */

#include "assert_macros.h"
#include "constant.h"
#include "coloredsquare.h"

ColoredSquare::ColoredSquare(QSFMLWidget *displayWidget, float *dt) : /*{{{*/
		Square(displayWidget, dt),
		m_shape(sf::Shape::Rectangle(0, 0,
		                             SQUARE_SIZE, SQUARE_SIZE,
		                             sf::Color::Color(sf::Randomizer::Random(0,255),
		                                              sf::Randomizer::Random(0,255),
		                                              sf::Randomizer::Random(0,255),
		                                              sf::Randomizer::Random(180,255)))),
		m_time_elapsed_on_destruction(0.f)
{
} /*}}}*/

ColoredSquare::ColoredSquare(QSFMLWidget *displayWidget, float *dt, sf::Color color) : /*{{{*/
	    Square(displayWidget, dt),
	    m_shape(sf::Shape::Rectangle(0, 0, SQUARE_SIZE, SQUARE_SIZE, color)),
	    m_time_elapsed_on_destruction(0.f)
{
} /*}}}*/

ColoredSquare::~ColoredSquare() /*{{{*/
{
} /*}}}*/

void ColoredSquare::setColor(sf::Color color) /*{{{*/
{
	m_shape.SetColor(color);
} /*}}}*/

void ColoredSquare::setRandomColor() /*{{{*/
{
	m_shape.SetColor(sf::Color::Color(sf::Randomizer::Random(0,255),
	                                  sf::Randomizer::Random(0,255),
	                                  sf::Randomizer::Random(0,255),
	                                  sf::Randomizer::Random(180,255)));
} /*}}}*/

/************
 * DRAWABLE *
 ************/
/*{{{*/
void ColoredSquare::draw() const /*{{{*/
{
	displayer->Draw(m_shape);
} /*}}}*/

void ColoredSquare::update() /*{{{*/
{
	Square::update();
} /*}}}*/

// absolute setters
void ColoredSquare::setCenter(Vector2f center) /*{{{*/
{
	m_shape.SetCenter(center);
} /*}}}*/

void ColoredSquare::setPosition(Vector2f position) /*{{{*/
{
	m_shape.SetPosition(position);
} /*}}}*/

void ColoredSquare::setRotation(float angle) /*{{{*/
{
	m_shape.SetRotation(angle);
//    DEBUGGER("m_shape rotation : " << m_shape.GetRotation());
//    DEBUGGER("square rotation  : " << angle);
} /*}}}*/

// relative setters
void ColoredSquare::moveCenter(Vector2f dcenter) /*{{{*/
{
	Vector2f tmp = m_shape.GetCenter() + dcenter;
	m_shape.SetCenter(tmp);
} /*}}}*/

void ColoredSquare::move(Vector2f dposition) /*{{{*/
{
	m_shape.Move(dposition);
} /*}}}*/

void ColoredSquare::rotate(float dangle) /*{{{*/
{
	m_shape.Rotate(dangle);
} /*}}}*/

// getters
Vector2f ColoredSquare::getCenter() const /*{{{*/
{
	return m_shape.GetCenter();
} /*}}}*/

Vector2f ColoredSquare::getPosition() const /*{{{*/
{
	return m_shape.GetPosition();
} /*}}}*/

float ColoredSquare::getRotation() const /*{{{*/
{
	return m_shape.GetRotation();
} /*}}}*/

// description
std::string ColoredSquare::toString() const /*{{{*/
{
	std::ostringstream res;
	res << "position              : (" << getPosition().x << ","
	        << getPosition().y << ")" << std::endl;
	res << "center                : (" << getCenter().x << ","
	        << getCenter().y << ")" << std::endl;
	res << "angle                 : " << getRotation() << std::endl;
	res << "position in container : (" << getPositionInContainer().x << ","
	        << getPositionInContainer().y << ")" << std::endl;
	res << "color                 : (" << (int) m_shape.GetColor().r << ","
	        << (int) m_shape.GetColor().g << ","
	        << (int) m_shape.GetColor().b << ")" << std::endl;
	return res.str();
} /*}}}*/
/*}}}*/

/***********
 * ACTIONS *
 ***********/
/*{{{*/
void ColoredSquare::destroyStartingProcess() /*{{{*/
{

} /*}}}*/

void ColoredSquare::destroyProcess() /*{{{*/
{
	if (m_time_elapsed_on_destruction <= SQUARE_DESTRUCTION_DELAY)
	{
	    destroy.maintain();
	    rotate((*dt) * SQUARE_ROTATION_SPEED);
	    m_time_elapsed_on_destruction += *dt;
	    m_shape.SetScale(Vector2f(1.f,1.f)/(1 + m_time_elapsed_on_destruction));
	}
} /*}}}*/

void ColoredSquare::destroyStoppingProcess() /*{{{*/
{
} /*}}}*/
/*}}}*/

